/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import Common.LoginFailedException;
import Common.Message;
import Server.MailServer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import persistence.DataManager;
/**
 *
 * @author Samsung
 */
public class MailClient {
   private String user;
   private MailServer server;
    /**
     * This list will contains the current messages list of this mailclient
     * based on its username denoted by
     * <code>usuario</code>
     *
     * @param user
     * @param server
     * @autor Wilson Rolando Crespo Omonte
     */
    public MailClient(String user, String serverName) {
        this.user = null;
        this.server = findServer(serverName);

        createServerIfConcerns(user, serverName);

        try {
            loginUser(user, server);
            
        } catch (LoginFailedException ex) {
            Logger.getLogger(MailClient.class.getName()).log(Level.SEVERE, null, ex.getMessage());
        }

    }
    
    private void createServerIfConcerns(String user, String serverName) {
        if (server == null) {
            server = new MailServer(user, serverName);
        }
    }
    
    private boolean loginUser(String user, MailServer server) throws LoginFailedException {
        if (server.findUser(user)) {
            this.user = user;
            return true;
        }
        throw new LoginFailedException();
    }
   public int checkMessages() {        
        return server.getMessagesWaiting(user);
    }
    public Message readNextMessage() {
        return server.getNextMessage(user);
    }
    public void sendMessage(Message message) {
        server.post(message);
    }    
    public String buscar()
    {
        String nombre;        
        ArrayList<String> list=(ArrayList<String>) server.getRegisteredUsers();
        int i=0;        
        int contador=list.size();
        while(i<contador && (user == null ? list.get(i) != null : !user.equals(list.get(i)))) 
        {
            i=i+1;
        }                
        nombre=verificacion(list, i);        
        return nombre;        
    }        
    private String verificacion(ArrayList<String> list,int i)
    {        
        if(i==list.size())
        {
            return "";
        }
        return list.get(i);
    }
    public void deleteMessage(Message message) {
        server.deleteMessage(message);
    }
    
    
     private MailServer findServer(String serverName) {
        DataManager dataManager = null;
        try {
            dataManager = new DataManager();
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(MailClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            //return new DataManager<MailServer>(serverName + ".xml").find(1387269396776);
            DataManager<MailServer> dm = new DataManager<MailServer>(serverName + ".xml");
            dm.load();
            List<MailServer> result = (List<MailServer>) dm.returnForcedObject();
            if (result != null && result.size() > 0) {
                this.server = result.get(0);
                return result.get(0);
            }
            return null;
           
            
//            DataManager<MailServer> ms = new DataManager<MailServer>(serverName + ".xml");
//            return ms.find(server);
//            UserMessages userMessages = new UserMessages("anonymous");
            //System.out.println(dm.find(userMessages));
            //return (MailServer) dataManager.find(serverName+".xml");
        } catch (IOException ex) {
            Logger.getLogger(MailClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
           Logger.getLogger(MailClient.class.getName()).log(Level.SEVERE, null, ex);
       }
        return null;
    }
     
     public List<Message> listMessages()
     {
         return server.getNewMessages(user);
         
         
         
     }
     
     public boolean findUser()
     {
         return server.findUser(user);
     }
     
     
}

